<template>
  <div>
    <vxe-toolbar
      id="toolbar_demo3"
      :setting="{storage: true}"
      export
      :buttons="toolbarButtons"
      :refresh="{query: findList}"
    />
    <vxe-table
      ref="xTable"
      border
      resizable
      stripe
      highlight-current-row
      highlight-hover-row
      show-overflow
      row-id="id"
      :loading="loading"
      class="mytable-footer"
      height="700"
      :footer-method="footerMethod"
      :footer-cell-class-name="footerCellClassName"
      :data="tableData"
    >
      <vxe-table-column>
        <vxe-table-column
          field="mainOrderNumber"
          title="货源单号"
          show-header-overflow
          show-overflow
          min-width="160"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input v-model="message" placeholder="请输入货源单号" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="orderDetailNumber"
          title="运输单号"
          show-header-overflow
          show-overflow
          min-width="160"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input v-model="message" placeholder="请输入运输单号" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="statustype"
          title="状态"
          show-header-overflow
          show-overflow
          min-width="160"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input v-model="message" placeholder="请输入司机/车队名称" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="license_number"
          title="车牌号"
          show-header-overflow
          show-overflow
          min-width="160"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input v-model="message" placeholder="请输入司机电话" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="etc_amount"
          title="ETC总额"
          show-header-overflow
          show-overflow
          min-width="160"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input v-model="message" placeholder="请输入车牌号" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="shipper_name"
          title="装货人"
          show-header-overflow
          show-overflow
          min-width="160"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input v-model="message" placeholder="请输入入驻商名称" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="shipper_phone"
          title="装货电话"
          show-header-overflow
          show-overflow
          min-width="160"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input v-model="message" placeholder="请输入入驻商电话" clearable />
        </template>
      </vxe-table-column>

      <vxe-table-column>
        <vxe-table-column
          field="shipper_address"
          title="装货地址"
          show-header-overflow
          show-overflow
          min-width="160"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input v-model="message" placeholder="请输入合同编号" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="consignee_name"
          title="收货人"
          show-header-overflow
          show-overflow
          min-width="160"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input v-model="message" placeholder="请输入出发地" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="consignee_phone"
          title="收货电话"
          show-header-overflow
          show-overflow
          min-width="160"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input v-model="message" placeholder="请输入出发地" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="consignee_address"
          title="收货地址"
          show-header-overflow
          show-overflow
          min-width="160"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input v-model="message" placeholder="请输入出发地" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="sign_time"
          title="卸货时间"
          show-header-overflow
          show-overflow
          min-width="420"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-date-picker
            v-model="value2"
            type="datetimerange"
            :picker-options="pickerOptions"
            style="width:100%"
            range-separator="至"

            start-placeholder="开始日期"
            end-placeholder="结束日期"
            align="right"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd HH:mm:ss"
            @change="dataPicker"
          ></el-date-picker>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="create_time"
          title="创建时间"
          show-header-overflow
          show-overflow
          min-width="420"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-date-picker
            v-model="value2"
            type="datetimerange"
            :picker-options="pickerOptions"
            style="width:100%"
            range-separator="至"

            start-placeholder="开始日期"
            end-placeholder="结束日期"
            align="right"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd HH:mm:ss"
            @change="dataPicker"
          ></el-date-picker>
        </template>
      </vxe-table-column>
      <vxe-table-column show-overflow title="操作" show-header-overflow width="100" fixed="right">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="详情" placement="top">
            <vxe-button type="text" @click="funcDetail(scope.row.code)">
              <i class="el-icon-document" />
            </vxe-button>
          </el-tooltip>
        </template>
      </vxe-table-column>
    </vxe-table>

    <vxe-pager
      :loading="loading"
      :current-page.sync="listQuery.page"
      :page-size.sync="listQuery.limit"
      :total="listQuery.totalzong"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
      @page-change="handlePageChange"
    />
  </div>
</template>
<script>
import Vue from "vue";
import "xe-utils";
import VXETable from "vxe-table";
import "vxe-table/lib/index.css";
import { Script } from "vm";
import request from "@/utils/request";
import { fetchList } from "@/api/article";
import Pagination from "@/components/Pagination";
import Sortable from "sortablejs";
import XEUtils from "xe-utils";
import VXEUtils from "vxe-utils";
Vue.use(VXETable, VXEUtils, XEUtils, {
  mounts: ["cookie"]
});
export default {
  data() {
    return {
      message: "",
      loading: false,
      tableData: [],
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      },

      value2: "",
      detailFlag: false,
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        totalzong: 0,
        sortField: "",
        sortType: ""
      },
      toolbarButtons: [
        {
          code: "btn1",
          name: "查询"
        }
        // {
        //   name: '下拉按钮',
        //   dropdowns: [{
        //       name: '按钮111',
        //       code: 'btn2'
        //     },
        //     {
        //       name: '按钮222',
        //       code: 'btn3'
        //     }
        //   ]
        // }
      ]
    };
  },
  components: {},
  created() {
    this.findList();
  },
  methods: {
    findList() {
      this.loading = true;
      return new Promise(resolve => {
        request({
          url: "/arpa-basic-api/openInsure",
          method: "get",
          params: this.listQuery
        }).then(response => {
          this.tableData = response.data.records;
          this.listQuery.total = response.data.total;
          this.loading = false;
          resolve();
        });
      });
    },
    // 获取详情
    funcDetail(code) {
      this.detailFlag = true;
      this.$refs.detailRef.getContract(code);
    },
    // 创建时间
    dataPicker(value) {
      console.log(value);
      if (value) {
        this.listQuery.gmtCreatedStart = value[0];
        this.listQuery.gmtCreatedEnd = value[1];
      } else {
        this.listQuery.gmtCreatedStart = "";
        this.listQuery.gmtCreatedEnd = "";
      }
    },
    handlePageChange({ currentPage, pageSize }) {
      this.listQuery.page = currentPage;
      this.listQuery.limit = pageSize;
      this.findList();
    },
    footerCellClassName({ $rowIndex, column, columnIndex }) {
      if (columnIndex === 0) {
        if ($rowIndex === 0) {
          return "col-blue";
        } else {
          return "col-red";
        }
      }
    },
    footerMethod({ columns, data }) {
      return [
        columns.map((column, columnIndex) => {
          if (columnIndex === 0) {
            return "平均";
          }
          if (["pageviews", "forecast"].includes(column.property)) {
            return XEUtils.mean(data, column.property);
          }
          return null;
        }),
        columns.map((column, columnIndex) => {
          if (columnIndex === 0) {
            return "和值";
          }
          if (["pageviews", "forecast"].includes(column.property)) {
            return XEUtils.sum(data, column.property);
          }
          return null;
        })
      ];
    }
  }
};
</script>

<style>
.vxe-table .vxe-body--column:not(.col--ellipsis),
.vxe-table .vxe-footer--column:not(.col--ellipsis),
.vxe-table .vxe-header--column:not(.col--ellipsis) {
  padding: 6px 0;
}

.vxe-toolbar {
  padding: 0 0 10px 0;
}
</style>
